package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.AQIRealDataE;
import com.infore.airsupervise.biz.AQIRealDataBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * @features: 根据mn号获取实时数据 业务实现类
 * @author: WuTao
 * @date: 2017/3/17 09:23
 */
public class AQIRealDataBizImpl implements AQIRealDataBiz{

    @Override
    public Call sendAQIRealDataRequst(String mn, final OnServerResponseListener<AQIRealDataE> listener) {
        //请求参数
        Map<String,Object> params = new HashMap<>();
        params.put("mn",mn);
        //初始化请求对象
        Call<BaseResponse> call=HttpUtil.getHttpService().postParams(HttpAPI.GET_AQIREAL,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                //解析
                AQIRealDataE aqiRealDataE= JSONUtil.parseBean(response.getData(),Constants.RESPONSE_BEAN_KEY,AQIRealDataE.class);
                listener.onSucceed(aqiRealDataE,response.getMessage());
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }


        });
        return call;
    }
}
